/*
 * @(#)Parameter.java     14/08/02
 * 
 * Author             Rick Wilson
 * Copyright (c) 2014 Rick Wilson
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of 'Taareekah' nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */



package parameters;

//~--- non-JDK imports --------------------------------------------------------

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * A leaf node which holds a data value.
 * @author rick
 */
public class Parameter extends ParameterNode {

    /** The data value as a String */
    private String	value;

    /**
     * 
     * @param key
     * @param type
     */
    public Parameter(String key, String type) {
	this.key	= key;
	this.value	= null;
	this.parent	= null;
	this.type	= type;
    }

    /**
     * 
     * @param key
     * @param value
     * @param type
     */
    public Parameter(String key, String value, String type) {
	this.key	= key;
	this.value	= value;
	this.parent	= null;
	this.type	= type;
    }

    /**
     *
     * @param key
     * @param value
     * @param parent
     * @param type
     */
    public Parameter(String key, String value, ParameterNode parent, String type) {
	this.key	= key;
	this.value	= value;
	this.parent	= parent;
	this.type	= type;
    }

    /**
     * Returns a human readable String form of this node.
     *
     *
     * @return
     */
    @Override
    public String toString() {
	String	ret	= "Parameter Key: " + key + ", Type: " + type + ", Value: " + value;

	return ret;
    }

    /**
     * Stores val as a string.
     * @param <T> The data type.
     * @param val The value.
     */
    @Override
    public <T> void setValue(T val) {
	value	= String.valueOf(val);
    }

    /**
     * Returns an XMLElement representation of this node.
     * @param doc The document the element will belong to.
     * @return A new XMLElement
     * @throws Exception
     */
    @Override
    public Element createXMLElement(Document doc) throws Exception {
	Element	root	= doc.createElement("parameter");
	Attr	attr	= doc.createAttribute("key");

	attr.setValue(key);

	Attr	value_attr	= doc.createAttribute("value");

	value_attr.setValue(value);

	Attr	type_attr	= doc.createAttribute("type");

	type_attr.setValue(type);
	root.setAttributeNode(attr);
	root.setAttributeNode(value_attr);
	root.setAttributeNode(type_attr);

	return root;
    }

    /**
     *
     * @return
     */
    @Override
    public boolean isSet() {
	if (value == null) {
	    return false;
	}

	return true;
    }

    /**
     * Gets the value as a string.
     * @return The value as a string.
     * @throws Exception It won't
     */
    @Override
    public String getValueAsString() throws Exception {
	return value;
    }

    /**
     * Gets the value as a int.
     * @return The value as a int.
     * @throws Exception If Integer.parseInt fails.
     */
    @Override
    public int getValueAsInt() throws Exception {
	return Integer.parseInt(value);
    }

    /**
     * Gets the value as a long.
     * @return The value as a long.
     * @throws Exception If  Long.parseLong fails.
     */
    @Override
    public long getValueAsLong() throws Exception {
	return Long.parseLong(value);
    }

    /**
     * Gets the value as a float.
     * @return The value as a float.
     * @throws Exception If Float.parseFloat fails.
     */
    @Override
    public float getValueAsFloat() throws Exception {
	return Float.parseFloat(value);
    }

    /**
     * Gets the value as a boolean.
     * @return The value as a boolean.
     * @throws Exception If Boolean.parseBoolean fails.
     */
    @Override
    public boolean getValueAsBool() throws Exception {
	return Boolean.parseBoolean(value);
    }

    /**
     * Gets the value as a double.
     * @return The value as a double.
     * @throws Exception If Double.parseDouble fails.
     */
    @Override
    public double getValueAsDouble() throws Exception {
	return Double.parseDouble(value);
    }

    /**
     * Returns whether this is a Parameter
     * @return true
     */
    @Override
    public boolean isParameter() {
	return true;
    }

    /**
     * Sets this nodes value.
     * @param value The value.
     */
    public void setValue(String value) {
	this.value	= value;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
